package com.usian.student.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.usian.Verifyrealname;
import com.usian.student.feign.ClassFeignService;
import com.usian.student.pojo.TbClass;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import com.usian.common.pojo.ResultVo;
import com.usian.student.dao.StudentDao;
import com.usian.student.pojo.TbStudent;
import com.usian.student.service.StudentService;

import java.io.IOException;
import java.util.Map;

@Service
public class StudentServiceImpl extends ServiceImpl<StudentDao, TbStudent> implements StudentService {

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    @Autowired
    private StudentDao studentDao;

    @Autowired
    private ClassFeignService classFeignService;


    @Override
    public ResultVo register(TbStudent tbStudent) throws IOException {
//        Verifyrealname verifyrealname = new Verifyrealname();
//        Map result = verifyrealname.checkUSer(tbStudent.getCardNo(), tbStudent.getPhone(), tbStudent.getName());
//        if (result.get("code").equals("0")) {
        if (true) {
            //请求接口成功
            if (true) {
                //信息一致
                //判断手机验证码是否正确
                String redisCode = stringRedisTemplate.opsForValue().get(tbStudent.getPhone());
                if (!StrUtil.isEmptyIfStr(redisCode)) {
                    if (redisCode.equals(tbStudent.getCode())) {
                        //验证码重复
                        LambdaQueryWrapper<TbStudent> lambdaQueryWrapper = new LambdaQueryWrapper<>();
                        lambdaQueryWrapper.eq(TbStudent::getPhone ,tbStudent.getPhone());
                        long count= studentDao.selectCount(lambdaQueryWrapper);
                        if (count>0){
                            //改手机号已经注册
                            return new ResultVo(300, "该用户已经注册,不能重复注册", null);
                        }else {
                            //需要根据班级id来获取对应的项目经理id和班主任id
                           ResultVo resultVo= classFeignService.info(tbStudent.getClassId());
                           TbClass tbclass= (TbClass) resultVo.getData();
                           tbStudent.setHId(tbclass.getHId());
                           tbStudent.setTId(tbclass.getTId());
                            studentDao.insert(tbStudent);
                            return new ResultVo(200,"success",null);
                        }
                    } else {
                        return new ResultVo(300, "输入验证码错误", null);
                    }
                }else {
                    return new ResultVo(300, "验证码过期", null);
                }
            } else {
                return new ResultVo(300,"异常",null);
//                return new ResultVo(Integer.parseInt(result.get("code") + ""), result.get("msg") + "", null);
            }
        } else {
            //请求借口失败
            return new ResultVo(300,"异常",null);
//            return new ResultVo(Integer.parseInt(result.get("code") + ""), result.get("msg") + "", null);
        }
    }
}
